package com.longcai.piaojia.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.longcai.piaojia.R;
import com.longcai.piaojia.conn.LatestOrderToolsGet;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.util.List;

/**
 * Created by Administrator on 2017/1/9.
 */
public class LatestOrderAdapter extends ArrayAdapter<LatestOrderToolsGet.ToolsEntity> {

    private Context context;
    private LayoutInflater layoutInflater;
    private LatestAddOrMinus latestAddOrMinus;

    public LatestOrderAdapter(Context context, List<LatestOrderToolsGet.ToolsEntity> lists) {
        super(context, 0, lists);

        this.context = context;

        this.layoutInflater = LayoutInflater.from(context);

        latestAddOrMinus = (LatestAddOrMinus) context;

    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        LatestOrderToolsGet.ToolsEntity toolsEntity = getItem(position);

        ViewHolder viewHolder;

        if (convertView == null) {

            convertView = BoundViewHelper.boundView(viewHolder = new ViewHolder(), ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) layoutInflater.inflate(R.layout.item_latest_order_list, null)));

            convertView.setTag(viewHolder);

        } else {

            viewHolder = (ViewHolder) convertView.getTag();

        }

        viewHolder.latest_order_list_add_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                latestAddOrMinus.add(position);
            }
        });

        viewHolder.latest_order_list_min_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                latestAddOrMinus.minus(position);
            }
        });


        viewHolder.latest_order_list_name_tv.setText(toolsEntity.title);
        viewHolder.latest_order_list_price_tv.setText(toolsEntity.price + "/" + toolsEntity.attr);
        viewHolder.latest_order_list_count_tv.setText(toolsEntity.count + "");

        return convertView;

    }

    class ViewHolder {

        @BoundView(R.id.latest_order_list_name_tv) private TextView latest_order_list_name_tv;
        @BoundView(R.id.latest_order_list_price_tv) private TextView latest_order_list_price_tv;
        @BoundView(R.id.latest_order_list_count_tv) private TextView latest_order_list_count_tv;

        @BoundView(R.id.latest_order_list_add_rl) private RelativeLayout latest_order_list_add_rl;
        @BoundView(R.id.latest_order_list_min_rl) private RelativeLayout latest_order_list_min_rl;

    }

    public interface LatestAddOrMinus {

        void add(int pos);
        void minus(int pos);

    }

}
